using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ListaFacil
{
    public partial class frmCadastroTelefoneDisque : Form
    {
        private DataTable dtSecao;
        private DataTable dtDisque;
        private bool editMode = false;
        private BindingSource bsSecao;
        private BindingSource bsDisque;

        public frmCadastroTelefoneDisque()
        {
            InitializeComponent();
            dtSecao = Program.SLF.consultarSecoesDisqueCadastradas();
            bsSecao =bsDisque =  new BindingSource();
            cboSecao.DataSource = dtSecao;
            cboSecao.ValueMember = "id_secao_disque";
            cboSecao.DisplayMember = "nome";
            cboCidade.DataSource = Program.SLF.consultarCidadesCadastradas();
            cboCidade.ValueMember = "id";
            cboCidade.DisplayMember = "cidade";
            dtDisque = Program.SLF.consultarTelefoneDisqueCadastrados();
            dataGridView1.DataSource = dtDisque;
            bsDisque.DataSource = dtDisque;
            bindingNavigator1.BindingSource = bsDisque;


            HabilitaBotoes(true);
            
        }

        private void HabilitaBotoes(bool flagHabilita)
        {
            bindingNavigatorAddNewItem.Enabled = flagHabilita;
            bindingNavigatorDeleteItem.Enabled = flagHabilita;
            bindingNavigatorMoveFirstItem.Enabled = flagHabilita;
            bindingNavigatorMoveLastItem.Enabled = flagHabilita;
            bindingNavigatorMoveNextItem.Enabled = flagHabilita;
            bindingNavigatorMovePreviousItem.Enabled = flagHabilita;
            bindingNavigatorPositionItem.Enabled = flagHabilita;
            edittoolStripButton.Enabled = flagHabilita;
            saveToolStripButton.Enabled = !flagHabilita;
            canceltoolStripButton.Enabled = !flagHabilita;
            txtTitular.Enabled = !flagHabilita;
            txtTelefone.Enabled = !flagHabilita;
            cboCidade.Enabled = !flagHabilita;
            cboSecao.Enabled = !flagHabilita;
            dataGridView1.Enabled = flagHabilita;
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow != null)
            {
                txtTelefone.Text = dataGridView1.CurrentRow.Cells["numero"].Value.ToString();
                txtTitular.Text = dataGridView1.CurrentRow.Cells["nome"].Value.ToString();
                cboSecao.SelectedValue = dataGridView1.CurrentRow.Cells["id_secao_disque"].Value;
                cboCidade.SelectedValue = dataGridView1.CurrentRow.Cells["id_cidade"].Value;
                this.Navegacao(this, e);
            }
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            this.HabilitaBotoes(false);
        }

        private void EditToolStripButton_Click(object sender, EventArgs e)
        {
            this.HabilitaBotoes(false);
            this.editMode = true;
        }

        private void cancelToolStripButton_Click(object sender, EventArgs e)
        {
            this.HabilitaBotoes(true);
        }

        private void dtTesteRowDeleting(object sender, EventArgs e)
        {

            int idDisque = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["id_disque"].Value);

            String retorno = Program.SLF.removerTelefoneDisque(idDisque);

            if (retorno != "")
                MessageBox.Show(retorno, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            PreencheGrid();
            this.HabilitaBotoes(true);
           

        }
     
        private void PreencheGrid()
        {
            dtDisque = Program.SLF.consultarTelefoneDisqueCadastrados();
            dtSecao = Program.SLF.consultarSecoesDisqueCadastradas();
            bsSecao.DataSource = dtSecao;

            bindingNavigator1.BindingSource = bsDisque;
            dataGridView1.DataSource=dtDisque;
        }


        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            string retorno = "";
          

            if (this.editMode == false)
            {
                int numero = Convert.ToInt32(txtTelefone.Text.ToString().Replace("-",""));
                int idCidade= Convert.ToInt32(cboCidade.SelectedValue);
                int idSecao = Convert.ToInt32(cboSecao.SelectedValue);
                retorno = Program.SLF.inserirTelefoneDisque(idCidade,txtTitular.Text.ToString().Replace("'","''"),numero,idSecao);

                if (retorno != "")
                    MessageBox.Show(retorno, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                int idDisque = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["id_disque"].Value);
                
                retorno = Program.SLF.updateTelefoneDisque(idDisque, txtTitular.Text, Convert.ToInt32(txtTelefone.Text.ToString().Replace("-","")), Convert.ToInt32(cboCidade.SelectedValue.ToString()), Convert.ToInt32(cboSecao.SelectedValue.ToString()));
                if (retorno != "")
                    MessageBox.Show(retorno, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            PreencheGrid();
            this.HabilitaBotoes(true);
        }

        private void txtNome_TextChanged(object sender, EventArgs e)
        {
            if (this.editMode)
                this.HabilitaBotoes(false);
        }

        private void bindingNavigatorPositionItem_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsDigit(e.KeyChar) == false)
                e.Handled = true;
        }

        private void Navegacao(object sender, EventArgs e)
        {
            this.editMode = false;
        }

        private void Edicao(object sender, EventArgs e)
        {
            this.editMode = true;
        }

        private void frmCadastroSecaoDisque_Load(object sender, EventArgs e)
        {
            dtSecao = Program.SLF.consultarSecoesDisqueCadastradas();
            bsSecao.DataSource = dtSecao;
            bindingNavigator1.BindingSource = bsSecao;
            dataGridView1.DataSource = bsSecao;
            this.HabilitaBotoes(true);

        }
        
    }
}